package com.hub.controller.asset;


import com.hub.controller.BaseController;
import com.hub.dto.asset.SearchAssetBrandDto;
import com.hub.dto.asset.BrandInfoDto;
import com.hub.frame.annotation.RestAuth;
import com.hub.model.asset.BrandModel;
import com.hub.service.asset.IAssetBrandInfoService;
import com.hub.utils.ApiCodeEnum;
import com.hub.utils.ApiResultObject;
import com.hub.utils.KeyValueModel;
import com.hub.utils.PagingResultObject;
import com.hub.utils.enums.BrandGradeEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author hub
 */
@RestController
@RequestMapping("/brand")
public class BrandInfoController extends BaseController {

    @Autowired
    private IAssetBrandInfoService assetBrandInfoService;
//    public ApiResultObject getList(Page<AssetBrandInfo> page, AssetBrandInfoParam param){
//        IPage<AssetBrandInfo> list = assetBrandInfoService.getList(page);
//        return ApiResultObject.success(list);
//    }

    @ApiOperation("分页查询")
    @PostMapping("/list")
    public PagingResultObject getList(@RequestBody @Valid SearchAssetBrandDto param){
        return this.assetBrandInfoService.getList(param);
    }

    @ApiOperation("品牌详情")
    @GetMapping(value = "/{id}")
    public ApiResultObject<BrandModel> getBrandDetail(@PathVariable  Integer id){
        return this.assetBrandInfoService.getBrandDetail(id);
    }

    @ApiOperation("新增品牌")
    @PostMapping(value="/add")
    public ApiResultObject doAddAssetBrand(@RequestBody @Valid BrandInfoDto addModel, BindingResult bindingResult){
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }
        return this.assetBrandInfoService.doAddAssetBrand(addModel);
    }

    @ApiOperation("删除品牌")
    @DeleteMapping(value = "/{id}")
    public ApiResultObject doDeleteAssetsBrand(@PathVariable long id){
        return this.assetBrandInfoService.doDeleteAssetBrand(id);
    }

    @ApiOperation("修改品牌")
    @PutMapping(value = "/{id}")
    public ApiResultObject doModifyAssetBrand(@RequestBody @Valid BrandInfoDto modifyModel, BindingResult bindingResult){
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }
        return this.assetBrandInfoService.doModifyAssetBrand(modifyModel);
    }

    @RestAuth(isPublic = true)
    @ApiOperation("获取品牌等级列表")
    @GetMapping(value = "/grade/list")
    public ApiResultObject GetBrandGrades(){

        List<KeyValueModel> list = BrandGradeEnum.getMaps();
        return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"",list);
    }

    /**
     * 获取品牌下来列表数据源
     * @return
     */
    @ApiOperation("获取品牌下来列表数据源")
    @GetMapping(value = "/all/list")
    public ApiResultObject getBrandKeyValues() {
        return this.assetBrandInfoService.getBrandKeyValues();
    }

}
